package usaco.c2010.january.silver;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/*
 * p3564
 * USACO 2010 January - Silver
 */

public class p3564 {

	static int K, E, N, cost;
	static store[] ss;

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		K = scanner.nextInt();
		E = scanner.nextInt();
		N = scanner.nextInt();
		ss = new store[N];
		for (int i=0;i<N;++i) {
			int x, price, nitems;
			x = scanner.nextInt();
			nitems = scanner.nextInt();
			price = scanner.nextInt();
			ss[i] = new store(x, price, nitems, E);
		}
		Arrays.sort(ss, new Comparator<store>() {

			public int compare(store s1, store s2) {
				return s1.price - s2.price;
			}
			
		});
		cost = 0;
		for (int i=0;i<N;++i) {
			if (K > ss[i].nitems) {
				cost += ss[i].nitems * ss[i].price;
				K -= ss[i].nitems;
			}
			else {
				cost += K * ss[i].price;
				K = 0;
			}
		}
		System.out.println(cost);

	}

}

class store {
	int x; 
	int price;
	int nitems;
	store(int x, int price, int nitems, int dest) {
		this.x = x;
		this.nitems = nitems;
		this.price = price;
		this.price += dest - x;
	}
}
